package com.jyuxuan.recruitment.config.shiro;

import com.jyuxuan.recruitment.mapper.AdminMapper;
import com.jyuxuan.recruitment.pojo.Admin;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

public class AdminRealm extends AuthorizingRealm {

    @Autowired
    AdminMapper adminMapper;

    private final Logger logger = LoggerFactory.getLogger(AdminRealm.class);


    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("执行授权逻辑");
        //给资源进行授权
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        //不授权先不写
        return info;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken arg0) throws AuthenticationException {
        System.out.println("执行认证逻辑"); // 编写shiro判断逻辑，判断用户名和密码
        RoleToken token = (RoleToken) arg0; // 判断用户名
        Admin admin = adminMapper.findByNumber(token.getUsername());
        if (admin == null) { // 该用户不存在
            return null; // shiro底层会抛出UnKnowAccountException
        }
        return new SimpleAuthenticationInfo(admin, admin.getPassword(), "Admin"); // 判断密码
    }
}